/******************************************************************************
 * Copyright (C) 2016  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_SYSTEM_THREAD_WIN32_H_
#define NODE_SYSTEM_THREAD_WIN32_H_

#define WIN32_MEAN_AND_LEAN
#include <Windows.h>
#include <process.h>
#include <node/noncopyable.h>
#include <stdint.h>

namespace node {
    namespace system {
        class runnable;

        class thread_impl : private noncopyable
        {
        public:
            enum priority
            {
                PRIO_LOWEST_IMPL = THREAD_PRIORITY_LOWEST,
                PRIO_LOW_IMPL = THREAD_PRIORITY_BELOW_NORMAL,
                PRIO_NORMAL_IMPL = THREAD_PRIORITY_NORMAL,
                PRIO_HIGH_IMPL = THREAD_PRIORITY_ABOVE_NORMAL,
                PRIO_HIGHEST_IMPL = THREAD_PRIORITY_HIGHEST
            };
            typedef DWORD   id;

            thread_impl(void);
            ~thread_impl(void);

            bool start_impl(runnable* runnable);
            void join_impl(void);
            bool joinable_impl(void);
            bool set_priority_impl(int priority);
            id id_impl(void) const { return _id; }

        private:
            static unsigned int __stdcall proc(void*);

            id _id;
            runnable* _runnable;
            uintptr_t _handle;
        };

        class this_thread_impl : private noncopyable
        {
        public:
            static thread_impl::id id_impl(void) {
                return ::GetCurrentThreadId();
            }
            static void sleep_impl(long milliseconds) {
                ::Sleep((DWORD)milliseconds);
            }
        };
}}

#endif //NODE_SYSTEM_THREAD_WIN32_H_
